#include "widget.h"
#include "./ui_widget.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QFont>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 主体布局
    QHBoxLayout *mainLayout = new QHBoxLayout(this);

    // 左右面板
    QWidget *leftPanel = createLeftPanel();
    QWidget *rightPanel = createRightPanel();

    mainLayout->addWidget(leftPanel, 1);  // 左侧占比小
    mainLayout->addWidget(rightPanel, 3); // 右侧占比较大

    this->setLayout(mainLayout);
}

Widget::~Widget()
{
    delete ui;
}


QWidget* Widget::createLeftPanel()
{
    QWidget *leftWidget = new QWidget();
    QVBoxLayout *leftLayout = new QVBoxLayout(leftWidget);

    // 添加雷达站点信息容器
    QWidget *stationInfo = createStationInfoWidget();
    leftLayout->addWidget(stationInfo);

    // 第二容器：武器选择区域
    leftLayout->addWidget(createWeaponControlPanel());

    // 第三容器：滚动雷达信息列表
    leftLayout->addWidget(createScrollingInfoList());

    return leftWidget;
}

QWidget* Widget::createRightPanel()
{
    QLabel *rightLabel = new QLabel("Right Container (Radar View)");
    rightLabel->setStyleSheet("background-color: lightblue; border: 1px solid gray;");
    rightLabel->setAlignment(Qt::AlignCenter);

    return rightLabel;
}

QWidget* Widget::createStationInfoWidget()
{
    QWidget *stationInfoWidget = new QWidget();
    QHBoxLayout *stationInfoLayout = new QHBoxLayout(stationInfoWidget);

    // 左侧 logo
    QLabel *logoLabel = new QLabel();
    logoLabel->setPixmap(QPixmap(":/img/img/logo.jpg").scaled(64, 64, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    logoLabel->setFixedSize(70, 70);
    stationInfoLayout->addWidget(logoLabel);


    // 右侧信息块
    QVBoxLayout *infoLayout = new QVBoxLayout();

    QLabel *stationName = new QLabel("雷达站点：南海1号站");
    QLabel *status = new QLabel("存活状态：🟢 正常运行");
    QLabel *personnel = new QLabel("人员数量：12 人");

    QFont infoFont("Microsoft YaHei", 10);
    stationName->setFont(infoFont);
    status->setFont(infoFont);
    personnel->setFont(infoFont);

    stationName->setStyleSheet("color: #333;");
    status->setStyleSheet("color: green;");
    personnel->setStyleSheet("color: #333;");

    infoLayout->addWidget(stationName);
    infoLayout->addWidget(status);
    infoLayout->addWidget(personnel);

    stationInfoLayout->addLayout(infoLayout);
    stationInfoLayout->setContentsMargins(5, 5, 5, 5);

    stationInfoWidget->setStyleSheet("background-color: #f5f5f5; border: 1px solid #ccc; border-radius: 6px;");

    return stationInfoWidget;
}

QWidget* Widget::createScrollingInfoList()
{
    scrollArea = new QScrollArea();
    scrollArea->setWidgetResizable(true);
    scrollArea->setFixedHeight(150); // 高度你可以自调

    // 隐藏滚动条
    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    QWidget *scrollContent = new QWidget();
    scrollLayout = new QVBoxLayout(scrollContent);
    scrollLayout->setSpacing(10);
    scrollLayout->setContentsMargins(5, 5, 5, 5);

    // 添加多个元素（可根据真实数据生成）
    QStringList dataList = {
        "距离：12km | 高度：300m | 速度：250km/h",
        "距离：9km | 高度：200m | 速度：220km/h",
        "距离：15km | 高度：500m | 速度：300km/h",
        "距离：8km | 高度：150m | 速度：210km/h"
    };

    for (const QString &text : dataList) {
        QLabel *itemLabel = new QLabel(text);
        itemLabel->setStyleSheet("background:#fff; border:1px solid #ccc; border-radius:5px; padding:4px;");
        scrollLayout->addWidget(itemLabel);
    }

    scrollContent->setLayout(scrollLayout);
    scrollArea->setWidget(scrollContent);

    startScrollTimer(); // 启动滚动

    return scrollArea;
}

void Widget::startScrollTimer()
{
    scrollTimer = new QTimer(this);
    connect(scrollTimer, &QTimer::timeout, this, [=]() {
        if (!scrollLayout || scrollLayout->count() <= 1) return;

        QWidget *firstItem = scrollLayout->itemAt(0)->widget();

        // 动画向上滚动
        QPropertyAnimation *anim = new QPropertyAnimation(firstItem, "geometry");
        QRect startRect = firstItem->geometry();
        QRect endRect = QRect(startRect.x(), startRect.y() - startRect.height() - scrollLayout->spacing(),
                              startRect.width(), startRect.height());
        anim->setDuration(500);
        anim->setStartValue(startRect);
        anim->setEndValue(endRect);
        anim->start(QAbstractAnimation::DeleteWhenStopped);

        // 动画结束后将第一个元素移到最后
        connect(anim, &QPropertyAnimation::finished, this, [=]() {
            scrollLayout->removeWidget(firstItem);
            scrollLayout->addWidget(firstItem);
            firstItem->show();
        });
    });

    scrollTimer->start(2000); // 每2秒滚动一次
}

QWidget* Widget::createWeaponControlPanel()
{
    QWidget *weaponPanel = new QWidget();
    QVBoxLayout *layout = new QVBoxLayout(weaponPanel);
    layout->setContentsMargins(10, 0, 10, 0); // 左 * 右 *
    layout->setSpacing(2);

    QFont labelFont("Microsoft YaHei", 10);
    QString labelStyle = "color: #333;";
    QString comboStyle = "QComboBox { padding: 4px; border: 1px solid #aaa; border-radius: 4px; }";
    QString buttonStyle = "QPushButton { background-color: #0078D7; color: white; border: none; "
                          "padding: 6px 12px; border-radius: 4px; }"
                          "QPushButton:hover { background-color: #005A9E; }";

    // 武器选择
    QHBoxLayout *row1 = new QHBoxLayout();
    QLabel *weaponLabel = new QLabel("武器：");
    weaponLabel->setFont(labelFont);
    weaponLabel->setStyleSheet(labelStyle);
    QComboBox *weaponCombo = new QComboBox();
    weaponCombo->addItems({"导弹", "火箭弹", "激光炮"});
    weaponCombo->setStyleSheet(comboStyle);
    row1->addWidget(weaponLabel);
    row1->addWidget(weaponCombo);

    // 模式选择
    QHBoxLayout *row2 = new QHBoxLayout();
    QLabel *modeLabel = new QLabel("模式：");
    modeLabel->setFont(labelFont);
    modeLabel->setStyleSheet(labelStyle);
    QComboBox *modeCombo = new QComboBox();
    modeCombo->addItems({"手动", "自动", "跟踪"});
    modeCombo->setStyleSheet(comboStyle);
    row2->addWidget(modeLabel);
    row2->addWidget(modeCombo);

    // 发射按钮
    QPushButton *fireBtn = new QPushButton("发射");
    fireBtn->setStyleSheet(buttonStyle);
    fireBtn->setFont(QFont("Microsoft YaHei", 10, QFont::Bold));
    fireBtn->setFixedHeight(28); // 减小高度
    fireBtn->setCursor(Qt::PointingHandCursor);
    fireBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); // 宽度自动填满


    // 添加到主布局
    layout->addLayout(row1);
    layout->addLayout(row2);
    layout->addWidget(fireBtn);

    // 容器样式
    weaponPanel->setStyleSheet("background-color: #f0f0f0; border: 1px solid #ccc; border-radius: 6px;");

    return weaponPanel;
}
